package artid.com.arttogo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.AuctionArtist_Adapter;
import artid.com.arttogo.bean.AuctionArtistListParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * 
 *    
 * 项目名称：ArtworkArt   
 * 类名称：AuctionArtist_Activity   
 * 类描述：艺术家列表   
 * 创建人：耿卫斌   
 * 创建时间：2017-3-3 下午4:32:30   
 *
 */
public class AuctionArtist_Activity extends BaseActivity implements OnClickListener {
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private LinearLayout noDataLL;
	private boolean isRefreshed = false;// 是否刷新
	private int page = 1;
	private AuctionArtist_Adapter mAdapter;
	private String auctionID;//拍卖会ID
	private List<AuctionArtistListParser> list=new ArrayList<AuctionArtistListParser>();
	private List<AuctionArtistListParser> more_list=new ArrayList<AuctionArtistListParser>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_auctionartist);
		initView();
	}

	private void initView(){
		mContext=AuctionArtist_Activity.this;
		auctionID=getIntent().getStringExtra("auctionID");
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setOnClickListener(this);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mTittle.setText("艺术家");
		mListView = (PullableListView) findViewById(R.id.AuctionArtist_ListView);
		mListView.setOnItemClickListener(onItemClick);
		noDataLL = (LinearLayout) findViewById(R.id.ll_hint);
		mPullLayout = (PullToRefreshLayout) findViewById(R.id.AuctionArtist_refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// 下拉刷新
				page = 1;
				isRefreshed = true;
				getData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// 上拉加载
				page++;
				getData();
			}
		});
		getData();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;

		default:
			break;
		}
	}

	OnItemClickListener onItemClick=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
								long arg3) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(mContext,CollecterActivity.class);
			intent.putExtra("id",more_list.get(position).getArtist_id());
			mContext.startActivity(intent);
		}
	};

	/**
	 * 获取艺术家列表
	 */
	private void getData(){
		VolleyBasic.getInstance(this).post(Cache.auction_ArtistList,
				VolleyMap.getInstance().auction_ArtistList(page, 10, auctionID),
				AuctionArtistListParser.class, new VolleyCallback() {

			@SuppressWarnings({ "unchecked", "static-access" })
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
				try {
					if(page==1){
						//当请求第二页的时候num不会返回值
						JSONObject obj = new JSONObject(vd.getJson());
						String num=obj.optString("num");
						mTittle.setText("艺术家（"+num+"）");
					}
					list=(List<AuctionArtistListParser>) vd.getList();
					if (isRefreshed == true) {
						more_list.clear();
						isRefreshed = false;
					}
					if (!list.isEmpty()) {
						more_list.addAll(list);
					}

					//这里是显示有数据和无数据的布局
					if(more_list!=null && more_list.size()!=0){
						noDataLL.setVisibility(View.GONE);
					}else{
						noDataLL.setVisibility(View.VISIBLE);
						return;
					}

					if (mAdapter == null) {
						mAdapter=new AuctionArtist_Adapter(mContext, more_list);
						mListView.setAdapter(mAdapter);
					}
					mAdapter.notifyDataSetChanged();

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub
				mPullLayout.loadmoreFinish(mPullLayout.FAIL);
				//这里是显示有数据和无数据的布局
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
			}
		});
	}
}
